package com.ss.ttvideoengine;

import android.content.Context;
import android.media.AudioManager;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;

import com.ss.android.article.base.feature.video.VideoUrlDepend;
import com.ss.ttm.player.IMediaPlayerClient;
import com.ss.ttvideoengine.b.VideoDataContainer;
import com.ss.ttvideoengine.b.VideoItem;
import com.ss.ttvideoengine.c.HttpRequest;
import com.ss.ttvideoengine.c.b;
import com.ss.ttvideoengine.d.TTVideoEngineLog;
import com.ss.ttvideoengine.d.TTVideoError;
import com.ss.ttvideoengine.log.c;

import java.lang.ref.WeakReference;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public final class TTVideoEngine {
    final class a implements b {
        private final WeakReference<TTVideoEngine> mTTVideoEngineRef;

        public a(TTVideoEngine ttVideoEngine) {
            this.mTTVideoEngineRef = new WeakReference(ttVideoEngine);
        }

        public final void a() {
            TTVideoEngineLog.log("TTVideoEngine", "dns cancelled");
            Object v0 = this.mTTVideoEngineRef.get();
            if (v0 != null) {
                TTVideoEngine.f(((TTVideoEngine) v0));
            }
        }

        public final void a(TTVideoError ttVideoError) {
            if (ttVideoError == null) {
                TTVideoEngineLog.log("TTVideoEngine", String.format("fetcher should retry, error:%s", ttVideoError.toString()));
                TTVideoEngine ttVideoEngine = this.mTTVideoEngineRef.get();
                if (ttVideoEngine != null) {
                    if (ttVideoEngine.N.get(ttVideoEngine.mCurrentResolution) == 0) {
                        if (ttVideoError != null) {
                            ttVideoEngine.n.k.put("localdns", ttVideoError.toMap());
                        }
                    }
                    ttVideoEngine.n.mTTVideoErrorList.add(ttVideoError);
                }
            }
        }

        public final void a(String ip, TTVideoError ttVideoError) {
            TTVideoEngine ttVideoEngine = this.mTTVideoEngineRef.get();
            if (ttVideoEngine != null) {
                if (ttVideoError != null) {
                    TTVideoEngineLog.log("TTVideoEngine", String.format("dns failed:%s", ttVideoError.toString()));
                    if (ttVideoEngine.N.get(ttVideoEngine.mCurrentResolution).intValue() == 0) {
                        if (ttVideoError != null) {
                            ttVideoEngine.n.k.put("httpdns", ttVideoError.toMap());
                        }
                    }
                    TTVideoEngine.onVideoEngineFailed(ttVideoEngine, ttVideoError);
                } else {
                    TTVideoEngine.onDNSSuccess(ttVideoEngine, ip);
                }
            }
        }
    }

    public float A;
    public boolean B;
    private int mPreloadInterval;
    private int D;
    private boolean E;
    private boolean F;
    private int prevState;
    public int H;
    private com.ss.ttvideoengine.a.a I;
    private com.ss.ttvideoengine.c.c J;
    private Resolution mCurrentResolution;
    private Resolution mPrevResolution;
    private String[] M;
    private Map<Resolution, Integer> N;
    private String mHostNameURL;
    private boolean P;
    private boolean Q;
    private boolean R;
    public int retryCount;
    private boolean T;
    private TTVideoError mTTVideoError;
    public int prevPlaybackState;
    public int mDuration;
    public int mState;
    public String d;
    public HttpRequest e;
    public com.ss.ttvideoengine.aa f;
    public VideoListener mVideoListener;
    public FetchVideoInfoListener mFetchVideoInfoListener;
    public dd i;
    public Context mContext;
    public Surface mSurface;
    public SurfaceHolder mSurfaceHolder;
    public VideoDataContainer mVideoDataContainer;
    public c n;
    public IMediaPlayerClient mMediaPlayerClient;
    public Map p;
    public String mCurrentIp;
    public String mCurrentHost;
    public boolean s;
    public int t;
    public int u;
    public static int v = -1;
    public int w;
    public int x;
    public boolean isMute;
    public boolean z;

    public TTVideoEngine(Context context, int arg8) {
        int v5 = 2;
        boolean v1 = true;
        this.mPreloadInterval = 0;
        this.D = 3;
        this.E = false;
        this.F = false;
        this.prevPlaybackState = 0;
        this.prevState = 0;
        this.mDuration = 0;
        this.s = false;
        this.P = false;
        this.Q = false;
        this.R = false;
        this.t = 0;
        this.u = 0;
        this.w = v;
        this.x = 0;
        this.retryCount = 0;
        this.T = false;
        this.mTTVideoError = null;
        this.z = true;
        this.A = -1f;
        this.B = false;
        TTVideoEngineLog.log("TTVideoEngine", "init");
        this.mContext = context;
        this.isMute = false;
        this.z = true;
        this.mCurrentResolution = Resolution.Standard;
        this.N = new HashMap();
        this.N.put(Resolution.Standard, Integer.valueOf(0));
        this.N.put(Resolution.High, Integer.valueOf(0));
        this.N.put(Resolution.SuperHigh, Integer.valueOf(0));
        this.p = new HashMap();
        this.H = arg8;
        com.ss.ttm.player.m.a(6, false);
        boolean v0 = arg8 != v5 ? true : false;
        com.ss.ttm.player.m.a(1, v0);
        if (arg8 != 1) {
            v1 = false;
        }
        com.ss.ttm.player.m.a(v5, v1);
        com.ss.ttm.player.m.a(11, false);
        this.n = new c(new com.ss.ttvideoengine.log.c.a(this));
        this.n.a(this.mCurrentResolution.toString(), "");
    }

    public final void seekTo(int time, boolean arg8) {
        TTVideoEngineLog.log("TTVideoEngine", String.format("_seekTo:%d", Integer.valueOf(time)));
        if (this.mMediaPlayerClient != null) {
            if (!this.P) {
                this.g();
            }
            this.P = true;
            this.mMediaPlayerClient.seekTo(time);
            c v0 = this.n;
            if (!arg8) {
                if (v0.d) {
                    v0.c.lt = System.currentTimeMillis();
                } else {
                    v0.c.errorTime = System.currentTimeMillis();
                }
                v0.e();
                v0.c();
                v0.c = new com.ss.ttvideoengine.log.a();
            }
            v0.c.st = System.currentTimeMillis();
        } else {
            this.h();
        }
    }

    public final void setIsMute(boolean isMute) {
        if (this.mMediaPlayerClient != null) {
            if (this.mMediaPlayerClient.isOsPlayer()) {
                float volume = isMute ? 0f : 1f;
                this.mMediaPlayerClient.setVolume(volume, volume);
            } else {
                this.mMediaPlayerClient.setIsMute(isMute);
            }
        }
    }

    public final void a(VideoDataContainer videoDataContainer) {
        if (videoDataContainer == null) {
            this.onVideoEngineFailed(new TTVideoError("kTTVideoErrorDomainFetchingInfo", -9997));
            return;
        }
        String[] videoUrls = videoDataContainer.getSelectedResolutionVideoUrls(this.mCurrentResolution);
        Resolution[] resolutions = new Resolution[3];
        resolutions[0] = Resolution.Standard;
        resolutions[1] = Resolution.High;
        resolutions[2] = Resolution.SuperHigh;
        int index = this.mCurrentResolution.getIndex();
        String[] v0 = videoUrls;
        int v1_1;
        for (v1_1 = (index + 2) % 3; v1_1 != index; v1_1 = (v1_1 + 2) % 3) {
            if (v0 != null && v0.length != 0) {
                videoUrls = v0;
                break;
            }

            Resolution resolution = resolutions[v1_1];
            v0 = videoDataContainer.getSelectedResolutionVideoUrls(resolution);
            if (v0 != null && v0.length != 0) {
                this.mCurrentResolution = resolution;
                this.n.a(this.mCurrentResolution.toString(), "");
                videoUrls = v0;
                break;
            }
        }
        int v3_1 = ((Integer) this.N.get(this.mCurrentResolution)).intValue();
        TTVideoError ttVideoError = null;
        if (videoUrls == null || videoUrls.length == 0) {
            ttVideoError = new TTVideoError("kTTVideoErrorDomainFetchingInfo", -9997);
        } else if (videoUrls.length <= v3_1) {
            ttVideoError = new TTVideoError("kTTVideoErrorDomainFetchingInfo", -9996);
        }
        if (ttVideoError != null) {
            this.onVideoEngineFailed(ttVideoError);
            return;
        }
        TTVideoEngineLog.log("TTVideoEngine", String.format("current resolution:%s, urls:%s, index:%d",
                this.mCurrentResolution.toString(),
                TextUtils.join(",", videoUrls),
                v3_1));
        Resolution currentResolution = this.mCurrentResolution;
        int preloadInterval = 0;
        if (videoDataContainer.mVideoInfo != null) {
            VideoItem videoItem = videoDataContainer.mVideoInfo.selectDefinition(currentResolution.toString());
            if (videoItem != null) {
                preloadInterval = videoItem.preloadInterval;
            }
        }

        this.mPreloadInterval = preloadInterval;
        this.mHostNameURL = videoUrls[v3_1];
        this.M = videoUrls;
        this.p.put(this.mHostNameURL, "");
        this.j();
        String hostnameURL = this.mHostNameURL;
        TTVideoEngineLog.log("TTVideoEngine", String.format("hostnameURL:%s", hostnameURL));
        this.mState = 2;
        try {
            this.J = new com.ss.ttvideoengine.c.c(new URL(hostnameURL).getHost(), this.e);
            this.J.a(new a(this));
            this.J.b();
        } catch (Exception v0_6) {
        }
    }

    public final void startPlay(String host, String ip) {
        TTVideoEngineLog.log("TTVideoEngine", String.format("start to play video, host:%s, ip:%s", host, ip));
        this.mCurrentIp = ip;
        this.mCurrentHost = host;
        HashMap hashMap = new HashMap();
        hashMap.put("host", String.format(" %s", host));
        if (this.mMediaPlayerClient != null && this.mMediaPlayerClient.getType() == 0) {
            this.mMediaPlayerClient.release();
            this.mMediaPlayerClient = null;
        }

        if (this.mMediaPlayerClient == null) {
            this.mMediaPlayerClient = MediaPlayerClientProxy.createMediaPlayerClient(this.mContext);
            if (this.mMediaPlayerClient == null) {
                this.onError(new TTVideoError("kTTVideoErrorDomainVideoOwnPlayer", -9993, "create player failed"));
                return;
            } else {
                switch (this.mMediaPlayerClient.getType()) {
                    case 0:
                        this.H = 2;
                        break;
                    case 1:
                        this.H = 0;
                        break;
                    case 2:
                        this.H = 1;
                        break;
                }

                if (this.mMediaPlayerClient.getType() != 1) {
                    this.mMediaPlayerClient.getType();
                }

                if (this.mPreloadInterval != 0) {
                    this.mMediaPlayerClient.setIntOption(24, this.mPreloadInterval);
                }

                if (this.A >= 0f) {
                    this.setVolume(this.A, this.A);
                }

                this.mMediaPlayerClient.setIntOption(37, 0);
                this.mMediaPlayerClient.setIntOption(38, 0);
                this.mMediaPlayerClient.setIntOption(36, 0);
                this.mMediaPlayerClient.setIntOption(56, this.D);
                this.mMediaPlayerClient.setOnPreparedListener(new IMediaPlayerClient.OnPreparedListener(this));
                this.mMediaPlayerClient.setOnBufferingUpdateListener(new IMediaPlayerClient.OnBufferingUpdateListener(this));
                this.mMediaPlayerClient.setOnCompletionListener(new IMediaPlayerClient.OnCompletionListener(this));
                this.mMediaPlayerClient.setOnErrorListener(new IMediaPlayerClient.OnErrorListener(this));
                this.mMediaPlayerClient.setOnSeekCompleteListener(new IMediaPlayerClient.OnSeekCompleteListener(this));
                this.mMediaPlayerClient.setOnInfoListener(new IMediaPlayerClient.OnInfoListener(this));
                this.mMediaPlayerClient.setOnVideoSizeChangedListener(new IMediaPlayerClient.OnVideoSizeChangedListener(this));
                if (this.mSurfaceHolder != null) {
                    this.mMediaPlayerClient.setDisplay(this.mSurfaceHolder);
                }
                if (this.mSurface != null) {
                    this.mMediaPlayerClient.setSurface(this.mSurface);
                }
                this.mMediaPlayerClient.setScreenOnWhilePlaying();
                this.setIsMute(this.isMute);
                this.E = false;
            }
        } else {
            if (this.mTTVideoError != null || this.Q) {
                TTVideoEngineLog.log("TTVideoEngine", "mediaPlayer reset");
                this.mMediaPlayerClient.reset();
                if (this.mSurfaceHolder != null) {
                    this.mMediaPlayerClient.setDisplay(this.mSurfaceHolder);
                }
                if (this.mSurface != null) {
                    this.mMediaPlayerClient.setSurface(this.mSurface);
                }
                this.E = false;
                this.mTTVideoError = null;
            }
        }

        try {
            TTVideoEngineLog.log("TTVideoEngine", "set screen on");
            this.mMediaPlayerClient.setScreenOnWhilePlaying();
            this.mMediaPlayerClient.disableLooping();
            this.mMediaPlayerClient.setDataSource(this.mContext, Uri.parse(ip), ((Map) hashMap));
        } catch (Throwable throwable) {
            String domain = this.mMediaPlayerClient.isOsPlayer() ? "kTTVideoErrorDomainVideoOSPlayer" : "kTTVideoErrorDomainVideoOwnPlayer";
            this.onVideoEngineFailed(new TTVideoError(domain, -9992));
            return;
        }

        if (!this.E) {
            try {
                this.mMediaPlayerClient.prepareAsync();
            } catch (Exception exception) {
                exception.printStackTrace();
                String domain = this.mMediaPlayerClient.isOsPlayer() ? "kTTVideoErrorDomainVideoOSPlayer" : "kTTVideoErrorDomainVideoOwnPlayer";
                this.onVideoEngineFailed(new TTVideoError(domain, -9992));
                return;
            }
            if (this.mVideoListener != null) {
                this.mVideoListener.onPrepare();
            }
        } else {
            this.resumeVideo();
        }
        this.mState = 3;
    }

    public final void configResolution(Resolution resolution) {
        if (resolution != null) {
            TTVideoEngineLog.log("TTVideoEngine", String.format("config reolution:%s", resolution.toString()));
            if (this.mState != 0 && this.mState != 1) {
                if (this.mState == 3) {
                    if (this.mCurrentResolution == resolution) {
                        TTVideoEngineLog.log("TTVideoEngine", String.format("switch to the same resolution:%s, drop", resolution.toString()));
                    } else {
                        this.mPrevResolution = this.mCurrentResolution;
                        this.mCurrentResolution = resolution;
                        TTVideoEngineLog.log("TTVideoEngine", String.format("will switch to resolution:%s, from resolution:%s", this.mCurrentResolution.toString(), this.mPrevResolution.toString()));
                        c v0 = this.n;
                        String currentResolutionString = this.mCurrentResolution.toString();
                        String preResolutionString = this.mPrevResolution.toString();
                        v0.c.errorTime = System.currentTimeMillis();
                        v0.e();
                        v0.c = new com.ss.ttvideoengine.log.a();
                        v0.preResolutionString = preResolutionString;
                        v0.currentResolutionString = currentResolutionString;
                        this.Q = true;
                        this.t = this.getCurrentPosition();
                        if (this.mMediaPlayerClient != null) {
                            this.mMediaPlayerClient.pause();
                        }

                        this.a(this.mVideoDataContainer);
                    }
                }
                return;
            }
            this.mPrevResolution = resolution;
            this.mCurrentResolution = resolution;
            this.n.a(resolution.toString(), resolution.toString());
        }
    }

    public final void setVolume(float arg6, float arg7) {
        float v1;
        float v0 = 1f;
        if (this.mMediaPlayerClient != null) {
            if (this.mMediaPlayerClient.isOsPlayer()) {
                if (arg6 != 0f) {
                    v1 = v0;
                    arg7 = v0;
                } else {
                    v1 = arg6;
                }
                AudioManager audioManager = (AudioManager) this.mContext.getSystemService(Context.AUDIO_SERVICE);
                if (audioManager != null) {
                    audioManager.setStreamVolume(3, ((int) arg6), 0);
                }
            } else {
                v1 = arg6;
            }
            this.mMediaPlayerClient.setVolume(v1, arg7);
        }
    }

    public final void pause() {
        int v2 = 2;
        TTVideoEngineLog.log("TTVideoEngine", "pause");
        this.B = false;
        TTVideoEngineLog.log("TTVideoEngine", "_pause");
        if (!this.E) {
            this.F = true;
            this.onPlaybackStateChanged(v2);
        } else if (this.mMediaPlayerClient != null) {
            TTVideoEngineLog.log("TTVideoEngine", "player will pause");
            this.mMediaPlayerClient.pause();
            this.onPlaybackStateChanged(v2);
        }
    }

    public static int setDuration(TTVideoEngine ttVideoEngine, int duration) {
        ttVideoEngine.mDuration = duration;
        return duration;
    }

    public static VideoDataContainer setVideoDataContainer(TTVideoEngine ttVideoEngine, VideoDataContainer videoDataContainer) {
        ttVideoEngine.mVideoDataContainer = videoDataContainer;
        return videoDataContainer;
    }

    public static void onFetchedVideoInfoFailed(TTVideoEngine ttVideoEngine, TTVideoError arg3) {
        ttVideoEngine.n.a(null, arg3);
    }

    public static void onFetchVideoInfoCancelled(TTVideoEngine ttVideoEngine, String arg4) {
        c v0 = ttVideoEngine.n;
        if (arg4 != null) {
            v0.l = v0.l + arg4;
        }
    }

    public static int a(TTVideoEngine arg1) {
        return arg1.H;
    }

    private void onPlaybackStateChanged(int state) {
        if (this.prevPlaybackState != state) {
            TTVideoEngineLog.log("TTVideoEngine", String.format("playback state changed, prev:%d, current:%d", Integer.valueOf(this.prevPlaybackState), Integer.valueOf(state)));
            if (state == 1 && !this.R) {
                this.k();
                this.R = true;
                this.n.a();
            }
            this.prevPlaybackState = state;
            if (this.mVideoListener != null) {
                this.mVideoListener.onPlaybackStateChanged(this.prevPlaybackState);
            }
        }
    }

    private void onVideoEngineFailed(TTVideoError arg10) {
        HashMap v2;
        String v0_2;
        c v1_1;
        int v0;
        int v3 = 3;
        int v4 = 2;
        if (this.s) {
            this.mState = 0;
        } else {
            this.t = this.getCurrentPosition();
            ++this.retryCount;
            this.mState = 4;
            TTVideoEngineLog.log("TTVideoEngine", String.format("videoEngine failed:%s", arg10.toString()));
            if (this.retryCount >= v3) {
                TTVideoEngineLog.log("TTVideoEngine", "videoEngine retry failed");
                this.onError(arg10);
            } else {
                this.T = true;
                if ((arg10.domain.equals("kTTVideoErrorDomainHTTPDNS")) || (arg10.domain.equals("kTTVideoErrorDomainLocalDNS"))) {
                    v0 = v4;
                } else if (!arg10.domain.equals("kTTVideoErrorDomainFetchingInfo")) {
                    if (arg10.domain.equals("kTTVideoErrorDomainVideoOwnPlayer")) {
                        v0 = arg10.code == -499988 || arg10.code == -499987 || arg10.code == -499986 || arg10.code == -499985 || arg10.code == -499899 || arg10.code == -499898 || arg10.code == -499897 || arg10.code == -499896 || arg10.code == -499894 || arg10.code == -499893 || arg10.code == -499891 || arg10.code == 251658241 || arg10.code == -499799 || arg10.code == -499795 || arg10.code == -499794 || arg10.code == -499793 || arg10.code == -499792 ? 1 : 0;
                        if (v0 != 0) {
                            v0 = v4;
                        } else if (!arg10.b()) {
                            v0 = v4;
                        } else {
                            v0 = v3;
                        }
                    } else {
                        v0 = v4;
                    }
                } else if (arg10.code == -9996) {
                    v0 = 1;
                } else {
                    v0 = 0;
                }
                int v5 = this.retryCount == v4 ? 1 : v0;
                TTVideoEngineLog.log("TTVideoEngine", String.format("retry strategy:%d", Integer.valueOf(v5)));
                if (v5 != 0) {
                    c v0_1 = this.n;
                    if (((arg10.domain.equals("kTTVideoErrorDomainVideoOwnPlayer")) || (arg10.domain.equals("kTTVideoErrorDomainVideoOSPlayer"))) && v0_1.h.size() <= 1) {
                        ++v0_1.c.br;
                    }

                    HashMap v1 = arg10.toMap();
                    v1.put("strategy", Integer.valueOf(v5));
                    v0_1.g.add(v1);
                }

                if (!arg10.domain.equals("kTTVideoErrorDomainHTTPDNS") && !arg10.domain.equals("kTTVideoErrorDomainLocalDNS") && v5 == v4 && ((Integer) this.N.get(this.mCurrentResolution)).intValue() == 0) {
                    v1_1 = this.n;
                    v0_2 = this.mHostNameURL;
                    if (arg10 != null) {
                        v2 = arg10.toMap();
                        if (v0_2 == null) {
                            v0_2 = "";
                        }

                        v2.put("url", v0_2);
                        v1_1.k.put("cdn", v2);
                    }
                }

                if (v5 == v3) {
                    v1_1 = this.n;
                    v0_2 = this.mHostNameURL;
                    if (arg10 != null) {
                        v2 = arg10.toMap();
                        if (v0_2 == null) {
                            v0_2 = "";
                        }

                        v2.put("url", v0_2);
                        v1_1.k.put("player", v2);
                    }
                }

                switch (v5) {
                    case 0: {
                        label_111:
                        this.onError(arg10);
                        return;
                    }
                    case 1: {
                        this.e();
                        return;
                    }
                    case 2: {
                        this.N.put(this.mCurrentResolution, Integer.valueOf(((Integer) this.N.get(this.mCurrentResolution)).intValue() + 1));
                        this.a(this.mVideoDataContainer);
                        return;
                    }
                    case 3: {
                        this.startPlay(this.mCurrentHost, this.mCurrentIp);
                        return;
                    }
                }

                return;
            }
        }
    }

    public final void stop() {
        TTVideoEngineLog.log("TTVideoEngine", "stop");
        this.B = false;
        TTVideoEngineLog.log("TTVideoEngine", String.format("_stop, mState:%d", Integer.valueOf(this.mState)));
        switch (this.mState) {
            case 0: {
                this.s = true;
                break;
            }
            case 1: {
                this.s = true;
                if (this.I == null) {
                    break;
                }
                com.ss.ttvideoengine.a.a v1 = this.I;
                synchronized (v1) {
                    v1.a.removeCallbacksAndMessages(null);
                    if (v1.e == null) {
                        break;
                    }
                    v1.e.onFetchCancelled("fetcher cancelled");
                    if (v1.b) {
                        break;
                    }

                    v1.b = true;
                    v1.f.cancel();
                    break;
                }
            }
            case 2: {
                this.s = true;
                if (this.J == null) {
                    break;
                }

                this.J.interrupt();
                break;
            }
            case 3: {
                this.s = true;
                break;
            }
        }
        if (this.mMediaPlayerClient != null && this.prevPlaybackState != 0) {
            this.mMediaPlayerClient.stop();
        }

        this.g();
        this.onPlaybackStateChanged(0);
    }

    public static void onVideoEngineFailed(TTVideoEngine ttVideoEngine, TTVideoError arg1) {
        ttVideoEngine.onVideoEngineFailed(arg1);
    }

    public static IMediaPlayerClient getMediaPlayerClient(TTVideoEngine ttVideoEngine) {
        return ttVideoEngine.mMediaPlayerClient;
    }

    public static void seekTo(TTVideoEngine ttVideoEngine, int time) {
        ttVideoEngine.seekTo(time, false);
    }

    public static void onFetchVideoInfoSuccess(TTVideoEngine ttVideoEngine, VideoDataContainer videoDataContainer) {
        HashMap<String, Integer> resolutionDurationMap = new HashMap();
        int[] durations = new int[]{0, 0, 0};
        VideoItem videoItem1 = videoDataContainer.mVideoInfo.mVideoItem1;
        if (videoItem1 != null) {
            durations[0] = videoItem1.videoDuration;
        }

        VideoItem videoItem2 = videoDataContainer.mVideoInfo.mVideoItem2;
        if (videoItem2 != null) {
            durations[1] = videoItem2.videoDuration;
        }

        videoItem2 = videoDataContainer.mVideoInfo.mVideoItem3;
        if (videoItem2 != null) {
            durations[2] = videoItem2.videoDuration;
        }
        resolutionDurationMap.put(Resolution.Standard.toString(), Integer.valueOf(durations[0]));
        resolutionDurationMap.put(Resolution.High.toString(), Integer.valueOf(durations[1]));
        resolutionDurationMap.put(Resolution.SuperHigh.toString(), Integer.valueOf(durations[2]));
        HashMap<String, Object> hashMap = new HashMap();
        hashMap.put("duration", Integer.valueOf(videoDataContainer.mVideoInfo.videoDuration));
        hashMap.put("size", resolutionDurationMap);
        ttVideoEngine.n.a(hashMap, null);
    }

    private void onError(TTVideoError ttVideoError) {
        int v0_1;
        c v1 = this.n;
        HashMap<String, Object> map = ttVideoError.toMap();
        map.put("strategy", Integer.valueOf(0));
        v1.g.add(map);
        if (v1.d) {
            v1.c.lt = System.currentTimeMillis();
        } else {
            if ((ttVideoError.domain.equals("kTTVideoErrorDomainVideoOwnPlayer")
                    || ttVideoError.domain.equals("kTTVideoErrorDomainVideoOSPlayer"))
                    && v1.h.size() <= 1) {
                ++v1.c.br;
            }
            v1.c.errorTime = System.currentTimeMillis();
        }
        com.ss.ttvideoengine.log.a v2 = v1.c;
        if (ttVideoError.domain.equals("kTTVideoErrorDomainFetchingInfo")) {
            v0_1 = 1000;
        } else if (ttVideoError.domain.equals("kTTVideoErrorDomainLocalDNS") || ttVideoError.domain.equals("kTTVideoErrorDomainHTTPDNS")) {
            v0_1 = 1001;
        } else if ((ttVideoError.domain.equals("kTTVideoErrorDomainVideoOwnPlayer")) && (ttVideoError.b())) {
            v0_1 = 1002;
        } else {
            v0_1 = 1003;
        }
        v2.errt = v0_1;
        v1.c.errorCode = ttVideoError.code;
        v1.e();
        v1.c();
        v1.c = new com.ss.ttvideoengine.log.a();
        if (this.mVideoListener != null) {
            this.mVideoListener.onError(ttVideoError);
        }
        this.retryCount = 0;
    }

    private void onLoadStateChanged(int currentState) {
        if (this.prevState != currentState) {
            TTVideoEngineLog.log("TTVideoEngine",
                    String.format("load state changed, prev:%d, current:%d",
                            Integer.valueOf(this.prevState),
                            Integer.valueOf(currentState)));
            if (currentState == 2 && this.R && !this.P) {
                c v0 = this.n;
                if (v0.h.size() <= 1) {
                    ++v0.c.bc;
                }
            }
            this.prevState = currentState;
            if (mVideoListener != null) {
                mVideoListener.onLoadStateChanged(this.prevState);
            }
        }
    }

    static void onDNSSuccess(TTVideoEngine ttVideoEngine, String ip) {
        String host = Uri.parse(ttVideoEngine.mHostNameURL).getHost();
        String v1 = ttVideoEngine.mHostNameURL.replaceFirst(host, ip);
        TTVideoEngineLog.log("TTVideoEngine", String.format("dns success, host:%s, ip:%s", host, ip));
        ttVideoEngine.p.put(ttVideoEngine.mHostNameURL, ip);
        ttVideoEngine.j();
        String hostNameURL = ttVideoEngine.mHostNameURL;
        if (ttVideoEngine.z) {
            ttVideoEngine.z = false;
            c v3 = ttVideoEngine.n;
            if (v3.c != null && hostNameURL != null) {
                v3.c.initial_url = hostNameURL;
            }
        }
        ttVideoEngine.startPlay(host, v1);
    }

    public final void c() {
        IMediaPlayerClient v6 = null;
        this.stop();
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.release();
            this.mMediaPlayerClient = v6;
        }

        if (this.n != null) {
            c v0 = this.n;
            if (!v0.d || v0.c.pt <= 0) {
                v0.c.errorTime = System.currentTimeMillis();
                v0.e();
            } else {
                v0.d();
            }
            v0.a = ((com.ss.ttvideoengine.log.c.a) null);
        }
    }

    public static void onPlaybackStateChanged(TTVideoEngine ttVideoEngine, int state) {
        ttVideoEngine.onPlaybackStateChanged(state);
    }

    public static c c(TTVideoEngine ttVideoEngine) {
        return ttVideoEngine.n;
    }

    public static TTVideoError c(TTVideoEngine ttVideoEngine, TTVideoError ttVideoError) {
        ttVideoEngine.mTTVideoError = ttVideoError;
        return ttVideoError;
    }

    public static void onFetchedVideoInfo(TTVideoEngine ttVideoEngine, VideoDataContainer videoDataContainer) {
        ttVideoEngine.a(videoDataContainer);
    }

    public final int d() {
        return this.Q ? this.t : this.getCurrentPosition();
    }

    public static FetchVideoInfoListener getFetchVideoInfoListener(TTVideoEngine ttVideoEngine) {
        return ttVideoEngine.mFetchVideoInfoListener;
    }

    public final void e() {
        this.mState = 1;
        this.N.put(Resolution.Standard, Integer.valueOf(0));
        this.N.put(Resolution.High, Integer.valueOf(0));
        this.N.put(Resolution.SuperHigh, Integer.valueOf(0));
        String v0 = null;
        if (this.f != null) {
            com.ss.ttvideoengine.aa v6 = this.f;
            int v0_1 = v6.b != null ? v6.b.sp() : 0;
            String v1 = v6.a;
            long v2 = v6.b != null ? v6.b.itemId : 0;
            v0 = VideoUrlDepend.urlWithVideoId(v0_1, v1, v2, v6.d, 1, v6.c);
        }

        if (v0 == null) {
            this.onError(new TTVideoError("kTTVideoErrorDomainFetchingInfo", -9999));
        } else {
            TTVideoEngineLog.log("TTVideoEngine", String.format("start to fetch video info:%s", v0));
            this.I = new com.ss.ttvideoengine.a.a(this.e);
            this.I.e = new com.ss.ttvideoengine.a.a.VideoInfoFetchCallback(this);
            com.ss.ttvideoengine.a.a v1_1 = this.I;
            v1_1.d = v0;
            v1_1.c = 0;
            v1_1.a();
        }
    }

    public static VideoListener getVideoListener(TTVideoEngine ttVideoEngine) {
        return ttVideoEngine.mVideoListener;
    }

    public final void resumeVideo() {
        TTVideoEngineLog.log("TTVideoEngine", "resumed video");
        if (this.mMediaPlayerClient != null) {
            this.mMediaPlayerClient.setScreenOnWhilePlaying();
            if (this.E) {
                this.mMediaPlayerClient.start();
                this.onPlaybackStateChanged(1);
            } else {
                this.F = false;
            }
        }
    }

    public static void f(TTVideoEngine arg1) {
        arg1.n.d();
    }

    static Resolution g(TTVideoEngine arg1) {
        return arg1.mCurrentResolution;
    }

    private void g() {
        if (this.mMediaPlayerClient != null) {
            int v0 = this.w;
            if (v0 != v) {
                int v1 = this.mMediaPlayerClient.getCurrentPosition();
                TTVideoEngineLog.log("TTVideoEngine", String.format("last:%d,current:%d", Integer.valueOf(v0), Integer.valueOf(v1)));
                this.x = v1 - v0 + this.x;
                this.w = v1;
            }
        }
    }

    static Map h(TTVideoEngine arg1) {
        return arg1.N;
    }

    private void h() {
        TTVideoEngineLog.log("TTVideoEngine", "seek complete");
        if (this.mMediaPlayerClient != null) {
            this.w = this.mMediaPlayerClient.getCurrentPosition();
        }

        if (this.P) {
            this.P = false;
            this.n.a();
        }

        if (this.Q) {
            this.Q = false;
        }

        if (this.i != null) {
            this.i.dismissLoading();
            this.i = null;
        }
    }

    public static int resetRetryCount(TTVideoEngine videoEngine) {
        videoEngine.retryCount = 0;
        return 0;
    }

    private int getCurrentPosition() {
        return this.mMediaPlayerClient == null ? 0 : this.mMediaPlayerClient.getCurrentPosition();
    }

    public static boolean j(TTVideoEngine arg1) {
        arg1.E = true;
        return true;
    }

    private void j() {
        ArrayList v2 = new ArrayList();
        int v1;
        for (v1 = 0; v1 <= ((Integer) this.N.get(this.mCurrentResolution)).intValue(); ++v1) {
            if (v1 >= this.M.length) {
                break;
            }

            HashMap v3 = new HashMap();
            String v0 = this.M[v1];
            v3.put("url", v0);
            v3.put("ip", this.p.get(v0));
            if (this.J != null) {
                v0 = this.J.g == com.ss.ttvideoengine.c.c.f ? "203.107.1.4" : "local";
                v3.put("dns", v0);
            }

            v2.add(v3);
        }

        this.n.h = v2;
    }

    public static boolean k(TTVideoEngine arg1) {
        return arg1.F;
    }

    private boolean k() {
        boolean v2_1;
        int v10 = 2;
        int v0 = 0;
        try {
            String v2 = this.mVideoDataContainer.mVideoInfo.validate;
            if (!TextUtils.isEmpty(((CharSequence) v2)) && !"0".equals(v2)) {
                if (this.mMediaPlayerClient != null) {
                    v2_1 = this.mMediaPlayerClient.isOsPlayer();
                } else if (this.H == v10) {
                    v2_1 = true;
                } else {
                    v2_1 = false;
                }

                if (v2_1) {
                } else {
                    String v3 = this.d;
                    v2 = this.mMediaPlayerClient.getStringOption(47);
                    HashMap v4 = new HashMap();
                    if (v2 != null) {
                        String[] v5 = v2.replaceAll(" ", "").split(",");
                        int v6 = v5.length;
                        int v2_2;
                        for (v2_2 = 0; v2_2 < v6; ++v2_2) {
                            String[] v7 = v5[v2_2].split(":");
                            if (v7.length == v10) {
                                ((Map) v4).put(v7[0], v7[1]);
                            }
                        }
                    }

                    if (v3.equals(((Map) v4).get("vid"))) {
                    } else {
                        v0 = 1;
                    }
                }
            }
            if (v0 == 0) {
                return true;
            }

            c v0_2 = this.n;
            TTVideoError v2_3 = new TTVideoError("kTTVideoErrorDomainVideoOwnPlayer", -9991, "header meta validate failed");
            v0_2.c.hijack = 1;
            v0_2.g.add(v2_3);
        } catch (NullPointerException v0_1) {
            Log.d("TTVideoEngine", "fetcher video model null");
        }

        return true;
    }

    public static boolean l(TTVideoEngine arg1) {
        return arg1.B;
    }

    public static int m(TTVideoEngine arg1) {
        return arg1.u;
    }

    public static int n(TTVideoEngine arg1) {
        arg1.u = 0;
        return 0;
    }

    public static void o(TTVideoEngine arg0) {
        arg0.g();
    }

    public static boolean p(TTVideoEngine arg1) {
        arg1.R = false;
        return false;
    }

    public static void q(TTVideoEngine arg1) {
        arg1.onLoadStateChanged(3);
    }

    public static TTVideoError r(TTVideoEngine ttVideoEngine) {
        return ttVideoEngine.mTTVideoError;
    }

    public static void s(TTVideoEngine ttVideoEngine) {
        ttVideoEngine.h();
    }

    public static void t(TTVideoEngine ttVideoEngine) {
        TTVideoEngineLog.log("TTVideoEngine", "start to render");
        ttVideoEngine.onPlaybackStateChanged(1);
        ttVideoEngine.onLoadStateChanged(1);
        if (ttVideoEngine.mMediaPlayerClient != null) {
            ttVideoEngine.w = ttVideoEngine.mMediaPlayerClient.getCurrentPosition();
        }
        if ((ttVideoEngine.Q) || (ttVideoEngine.T)) {
            if (ttVideoEngine.t != 0) {
                ttVideoEngine.seekTo(ttVideoEngine.t, ttVideoEngine.Q);
            }

            ttVideoEngine.T = false;
        }

        ttVideoEngine.retryCount = 0;
        if (ttVideoEngine.mVideoListener != null) {
            ttVideoEngine.mVideoListener.onRenderStart();
        }
    }

    public static void u(TTVideoEngine arg2) {
        TTVideoEngineLog.log("TTVideoEngine", "buffering start");
        arg2.onLoadStateChanged(2);
    }

    public static void v(TTVideoEngine arg2) {
        TTVideoEngineLog.log("TTVideoEngine", "buffering end");
        arg2.onLoadStateChanged(1);
    }
}

